package com.manner.groupon.service.groupon.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manner.groupon.common.BaseResponse;
import com.manner.groupon.common.ResultUtils;
import com.manner.groupon.mapper.GroupPurchasePackageMapper;
import com.manner.groupon.model.dto.grouponCoupons.CouponPackageDTO;
import com.manner.groupon.model.entity.GroupPurchasePackage;
import com.manner.groupon.service.groupon.GroupPurchasePackageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author manner
 * @description 针对表[tb_group_purchase_package(团购套餐表)]的数据库操作Service实现
 * @createDate 2024-10-13 14:26:25
 */
@Service
public class GroupPurchasePackageServiceImpl extends ServiceImpl<GroupPurchasePackageMapper, GroupPurchasePackage>
        implements GroupPurchasePackageService {

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public BaseResponse<String> savePackage(CouponPackageDTO data) {
        //全部删除
        remove(new QueryWrapper<GroupPurchasePackage>().eq("gcid", data.getId()));
        //准备容器
        List<GroupPurchasePackage> container = new ArrayList<>();
        //遍历
        data.getList().forEach(item -> {
            //转换字符串
            String json = JSONUtil.toJsonStr(item.getList());
            container.add(new GroupPurchasePackage(null, data.getId(), item.getTitle(), json, null, null, null));
        });
        //保存数据
        saveBatch(container);
        //返回数据
        return ResultUtils.success("配置成功");
    }
}




